/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package upc.dew.inscripcionalumnos.util;

import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;



/**
 *
 * @author MARTY
 */
public class ServerMail {

    // Remplazar los valores por los las credenciales de GMAIL
    public static final String GMAIL_USERNAME="dark.luigi.18@gmail.com";
    public static final String GMAIL_PASSWORD="elmarikon";

    
    public static boolean sendEmail(String replyTo, String from_email, String from_name, 
            String to_email, String subject, String body, String content_type, String bcc_email)
    {
        boolean success = false;

        try
        {
            Properties prop = new Properties();
            prop.put("mail.smtp.port", "25");
            prop.put("mail.smtp.socketFactory.fallback", "false");
            prop.put("mail.smtp.quitwait", "false");
            prop.put("mail.smtp.host", "smtp.gmail.com");
            prop.put("mail.smtp.auth", "true");
            prop.put("mail.smtp.starttls.enable", "true");
            Session session = Session.getDefaultInstance(prop);
            Message msg = new MimeMessage(session);
            msg.setSubject(subject);
            InternetAddress from = new InternetAddress(from_email, from_name);
            InternetAddress to = new InternetAddress(to_email);
            msg.addRecipient(Message.RecipientType.TO, to);
            // add bcc if not null
            if ((bcc_email != null) && (!bcc_email.equals("")))
            {
                msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc_email));
            }
            msg.setFrom(from);
            // set reply to email address
            InternetAddress[] replyToAddress = new InternetAddress[1];
            replyToAddress[0] = new InternetAddress(replyTo);
            msg.setReplyTo(replyToAddress);
            Multipart multipart = new MimeMultipart("related");
            BodyPart htmlPart = new MimeBodyPart();
            htmlPart.setContent(body,content_type);
            multipart.addBodyPart(htmlPart);
            msg.setContent(multipart);
            Transport transport = session.getTransport("smtp");
            transport.connect(GMAIL_USERNAME, GMAIL_PASSWORD);
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();

            success = true;

        } 
        catch (UnsupportedEncodingException ex)
        {
            success = false;            
        } 
        catch (MessagingException ex)
        {
            success = false;            
        }

        return success;

    }

}
